<?php
// ----------------------------------------------------------------------
// pn-commerce Shopping Cart Solution for PostNuke
// http://pncommerce.de
// http://www.sourceforge.net/projects/pn-commerce/
//
// It is based on PostKart 0.701QA from
//			Destin LeBlanc [ postkart@zenflesh.com ]
// ----------------------------------------------------------------------
// Source file:  $Source: /cvsroot/pn-commerce/pn-commerce/pncmlogapi.php,v $
//
// Last changes from:  $Author: landseer $
//
// Revision:           $Revision: 1.4 $
//
// Date of revision    $Date: 2004/09/12 11:17:13 $
//  
// ----------------------------------------------------------------------
// LICENSE
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// To read the license please visit http://www.gnu.org/copyleft/gpl.html
// ----------------------------------------------------------------------

require_once('modules/pncommerce/common.php');

/**
 * info
 *
 */
function pncommerce_cmlogapi_info()
{
    return ( array( 'description' => pnVarPrepForDisplay(_PNC_LOGMANAGEMENTDESC), 'button' => pnVarPrepForDisplay(_PNC_LOGMANAGEMENT) ) );
}


/**
 * main
 *
 */
function pncommerce_cmlogapi_main()
{
    if( !pnModAPILoad( 'pncommerce', 'admin' ) )
    {
        return showerrorpage("unable to load pnadminapi", __FILE__, __LINE__);
    }
    if( !pnModAPILoad( 'pncommerce', 'user' ) )
    {
        return showerrorpage("unable to load pnuserapi", __FILE__, __LINE__);
    }
    $smarty = new pnRender( 'pncommerce' );
    $smarty->caching = false;
    $LogLevels = array( _PNC_LOG_DEBUG         => _PNC_LOG_DEBUGTEXT,
                        _PNC_LOG_USERERROR     => _PNC_LOG_USERERRORTEXT,
                        _PNC_LOG_ADMINERROR    => _PNC_LOG_ADMINERRORTEXT,
                        _PNC_LOG_GENERAL       => _PNC_LOG_GENERALTEXT,
                        _PNC_LOG_USERWARNING   => _PNC_LOG_USERWARNINGTEXT,
                        _PNC_LOG_ADMINWARNING  => _PNC_LOG_ADMINWARNINGTEXT,
                        _PNC_LOG_NOTICE        => _PNC_LOG_NOTICETEXT,
                        _PNC_LOG_NOLOG         => _PNC_LOG_NOLOGTEXT );
    $smarty->assign( 'LogLevels', $LogLevels );
    $dolog = pnVarCleanFromInput( 'dolog' );

    if( $dolog )
    { 
        // do the action
        list( $mode, $filter, $loglevel, $logaction, 
              $startMonth, $startDay, $startYear,
              $endMonth, $endDay, $endYear )
              = pnVarCleanFromInput( 'mode', 'filter', 'loglevel', 'logaction',
                                     'startMonth', 'startDay', 'startYear',
                                     'endMonth', 'endDay', 'endYear' );
        if( $mode == 'bylevel' )
        {
            $op = array( ">", ">=", "=", "<=", "<" );
            
            list($dbconn, $tablename, $tablecolumn ) = pncOpenDB('pncommerce_log');
            $sql = "SELECT $tablecolumn[timestamp],
                           $tablecolumn[ip],
                           $tablecolumn[username],
                           $tablecolumn[level],
                           $tablecolumn[text]
                    FROM $tablename
                    WHERE $tablecolumn[level]".$op[$filter].(int)$loglevel;
            $result = $dbconn->Execute($sql);

            // check for errors or no rows found, and if so return 0
            if (($dbconn->ErrorNo() == 0) && (!$result->EOF) )
            {
                $logentries = array();
                for (; !$result->EOF; $result->MoveNext())
                {
                    $logentry = array();
                    list( $logentry['timestamp'],
                          $logentry['ip'],
                          $logentry['username'],
                          $level,
                          $logentry['text'] ) = $result->fields;;
                    $logentry['level'] = $LogLevels[$level];
                    array_push( $logentries, $logentry );
                } 
                $smarty->assign( 'LogEntries', $logentries );
                $smarty->assign( 'LogEntryCount', count( $logentries ) );
            }
            else
            {
                $smarty->assign( 'LogEntryCount', 0 );
            }
            pncCloseDB($result);
        }
        elseif( $mode == 'bydate' );
        {
        }
    }
    return $smarty->fetch( 'pncommerce_cmlogapi_main.html' );
}

?>
